package com.badibadi.mytools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.Serializable;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.Toast;

import com.badibadi.activity.InterestSelectActivity;
import com.badibadi.activity.MyDataActivity;
import com.badibadi.infos.Results;
import com.badibadi.infos.UserInfoModel;
import com.badibadi.uniclubber.R;
import com.lidroid.xutils.util.LogUtils;

//Loop static
public class UserDataSet {

	private Context context;

	public UserDataSet(Context ct) {
		context = ct;
	}

	android.os.Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case 1:
				if (msg.getData() != null) {
					if (msg.getData().getString("Retmsg")
							.indexOf("第三方登录") != -1) {
						Utils.showMessage(
								context,
								""
										+ context.getResources().getString(
												R.string.wx_txt_123));
					} else {
						// 这是为了防止第一次登录修改了资料后,提示修改兴趣爱好的还会出现
						if (Constants.model.getAmount() != null) {
							if (Integer.parseInt(Constants.model.getAmount()) <= 1) {
								Constants.model.setAmount(""
										+ (Integer.parseInt(Constants.model
												.getAmount()) + 2));
							}
						}

						if (msg.getData().getBoolean("Finish") == true) {
							Intent intent = new Intent(context,
									MyDataActivity.class);
							context.startActivity(intent);
							((Activity) context).finish();
						}
					}

				}
				break;
			case 2:
				Toast.makeText(context,
						context.getResources().getString(R.string.l_net_error),
						0).show();
				break;
			case 3:
				// 3要做的事情就是：

				try {
					if (msg.getData() != null) {
						if (msg.getData().getString("Retmsg")
								.indexOf("第三方登录") != -1) {
							Utils.showMessage(
									context,
									""
											+ context.getResources().getString(
													R.string.wx_txt_123));
						} else {
							if (msg.getData().getBoolean("Refresh") == true) {
								((MyDataActivity) (context)).Refresh();
								Toast.makeText(context,
										"" + msg.getData().getString("Retmsg"),
										0).show();
							}
						}
					}

				} catch (Exception e) {
					// TODO: handle exception

				}
				break;
			}
		};
	};

	/**
	 * 
	 * 
	 * @param userInfoModel
	 *            UserInfoModel类
	 * @param uid
	 *            用户上传id
	 * @param XiuGai
	 *            1只修改背景图片,2只修改头像,其他数都不修改
	 * @param path
	 *            XiuGai来判定修改哪张图片 path为那张图片的路径，每次修改只能一张
	 * @param finish
	 *            请求结束后是否finish掉当前界面 进入一个新的MyData界面
	 */
	public void UserDataSet(final UserInfoModel userInfoModel,
			final int XiuGai, final String path, final boolean finish) {
		Utils.showPrgress(context);
		new Thread(new Runnable() {

			@Override
			public void run() {

				AjaxParams params = new AjaxParams();
				params.put("uid", userInfoModel.getUid());
				params.put("nickName", userInfoModel.getNickName());
				params.put("typeid", userInfoModel.getTypeid());
				params.put("advisory_fees", userInfoModel.getAdvisory_fees());
				params.put("expert_typeid", userInfoModel.getExpert_typeid());//
				params.put("speciality", userInfoModel.getSpeciality());
				params.put("motto", userInfoModel.getMotto());
				params.put("area_id", userInfoModel.getArea_id());
				params.put("email", userInfoModel.getEmail());
				params.put("sex", userInfoModel.getSex());
				params.put("alipay", userInfoModel.getAlipay());
				params.put("birthday", userInfoModel.getBirthday());
				//    
				params.put("phone", userInfoModel.getPhone());
				params.put("alipayName", userInfoModel.getAlipayName());
				if (XiuGai == 1) {
					try {
						params.put("info_head", new File(path));// "/storage/sdcard1/DCIM/Camera/IMG_20140723_231753.jpg")

					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						//    
						   
					}
					params.put("head", userInfoModel.getHead());
					params.put("uploadBack", "1");
				} else if (XiuGai == 2) {
					try {
						params.put("head", new File(path));// "/storage/sdcard1/DCIM/Camera/IMG_20140723_231742.jpg"

					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						//    
						   
					}
					params.put("info_head", userInfoModel.getInfo_head());// "
					params.put("uploadHead", "1");

				} else {
					params.put("head", userInfoModel.getHead());
					params.put("info_head", userInfoModel.getInfo_head());
				}

				params.put("like_club", userInfoModel.getLike_club());
				params.put("unlike_club", userInfoModel.getUnlike_club());
				params.put("education", userInfoModel.getEducation());

				FinalHttp fh = new FinalHttp();
				fh.post(Constants.BadiUrl + "userDataSet", params,
						new AjaxCallBack<Object>() {

							@Override
							public void onLoading(long count, long current) {
								// TODO Auto-generated method stub
								super.onLoading(count, current);
								//    
								// "current-->"
								// + current);

							}

							@Override
							public void onSuccess(final Object t) {
								// TODO Auto-generated method stub

								new Thread(new Runnable() {

									@Override
									public void run() {
										// TODO Auto-generated method stub
										Utils.ExitPrgress(context);
										//    

										String content = (String) t;
										//    
										if (content != null) {
											//    
											Results results = Utils
													.checkResult_NNN(context,
															content);

											if (results != null
													&& results.getRetmsg() != null) {
												//    
												// "results.getRetmsg==>"
												// + results
												// .getRetmsg());
												try {
													Message msg = new Message();
													msg.what = 1;
													Bundle bun = new Bundle();
													bun.putString("Retmsg",
															results.getRetmsg());
													bun.putBoolean("Finish",
															finish);
													msg.setData(bun);

													handler.sendMessage(msg);

												} catch (Exception e) {
													// TODO: handle exception
												}
											} else {
												// Toast.makeText(context,
												// "输出结果为空", 1).show();

												//    
												handler.sendEmptyMessage(2);

											}
										} else {
											// Toast.makeText(context,"网络连接出错",
											// 1).show();
											handler.sendEmptyMessage(2);

										}
									}
								}).start();

								super.onSuccess(t);
							}
						});
			}
		}).start();

	}

	/**
	 * 
	 * 
	 * @param userInfoModel
	 *            UserInfoModel类
	 * @param uid
	 *            用户上传id
	 * @param XiuGai
	 *            1只修改背景图片,2只修改头像,其他数都不修改
	 * @param path
	 *            XiuGai来判定修改哪张图片 path为那张图片的路径，每次修改只能一张
	 * @param refresh
	 *            是否刷新数据
	 */
	public void UserDataSet2(final UserInfoModel userInfoModel,
			final int XiuGai, final String path, final boolean refresh) {
		Utils.showPrgress(context);
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				AjaxParams params = new AjaxParams();
				// LogUtils.d("userInfoModel"+userInfoModel);
				params.put("uid", userInfoModel.getUid());
				params.put("nickName", userInfoModel.getNickName());
				params.put("typeid", userInfoModel.getTypeid());
				params.put("advisory_fees", userInfoModel.getAdvisory_fees());
				params.put("expert_typeid", userInfoModel.getExpert_typeid());//
				params.put("speciality", userInfoModel.getSpeciality());
				params.put("motto", userInfoModel.getMotto());
				params.put("area_id", userInfoModel.getArea_id());
				params.put("email", userInfoModel.getEmail());
				params.put("sex", userInfoModel.getSex());
				params.put("alipay", userInfoModel.getAlipay());
				params.put("birthday", userInfoModel.getBirthday());
				//    
				params.put("phone", userInfoModel.getPhone());
				params.put("alipayName", userInfoModel.getAlipayName());
				if (XiuGai == 1) {
					try {
						params.put("info_head", new File(path));// "/storage/sdcard1/DCIM/Camera/IMG_20140723_231753.jpg")

					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						//    
						   
					}
					params.put("head", userInfoModel.getHead());
					params.put("uploadBack", "1");
				} else if (XiuGai == 2) {
					try {
						params.put("head", new File(path));// "/storage/sdcard1/DCIM/Camera/IMG_20140723_231742.jpg"

					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						//    
						   
					}
					params.put("info_head", userInfoModel.getInfo_head());// "
					params.put("uploadHead", "1");

				} else {
					params.put("head", userInfoModel.getHead());
					params.put("info_head", userInfoModel.getInfo_head());
				}

				params.put("like_club", userInfoModel.getLike_club());
				params.put("unlike_club", userInfoModel.getUnlike_club());
				params.put("education", userInfoModel.getEducation());
				//    
				FinalHttp fh = new FinalHttp();
				fh.post(Constants.BadiUrl + "userDataSet", params,
						new AjaxCallBack<Object>() {

							@Override
							public void onLoading(long count, long current) {
								// TODO Auto-generated method stub
								super.onLoading(count, current);
								//    
								// "current-->"
								// + current);

							}

							@Override
							public void onSuccess(final Object t) {
								// TODO Auto-generated method stub

								Utils.ExitPrgress(context);
								//    

								String content = (String) t;
								//    
								if (content != null) {
									//    
									//    
									Results results = Utils.checkResult_NNN(
											context, content);

									if (results != null
											&& results.getRetmsg() != null) {
										//    
										// + results.getRetmsg());
										try {
											Message msg = new Message();
											msg.what = 3;
											Bundle bun = new Bundle();
											bun.putString("Retmsg",
													results.getRetmsg());
											bun.putBoolean("Refresh", refresh);
											msg.setData(bun);

											handler.sendMessage(msg);

										} catch (Exception e) {
											// TODO: handle exception
										}
									} else {
										// Toast.makeText(context, "输出结果为空",
										// 1).show();

										handler.sendEmptyMessage(2);

									}
								} else {
									// Toast.makeText(context,"网络连接出错",
									// 1).show();
									handler.sendEmptyMessage(2);

								}

								super.onSuccess(t);
							}
						});
			}
		}).start();
	}

	/**
	 * 
	 * 
	 * @param userInfoModel
	 *            UserInfoModel类
	 * @param uid
	 *            用户上传id
	 * @param XiuGai
	 *            1只修改背景图片,2只修改头像,其他数都不修改
	 * @param path
	 *            XiuGai来判定修改哪张图片 path为那张图片的路径，每次修改只能一张
	 * @param refresh
	 *            是否刷新数据
	 */
	public void UserDataSet3(final UserInfoModel userInfoModel,
			final int XiuGai, final String path, final boolean refresh) {
		Utils.showPrgress(context);
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub

				AjaxParams params = new AjaxParams();
				params.put("uid", userInfoModel.getUid());
				params.put("nickName", userInfoModel.getNickName());
				params.put("typeid", userInfoModel.getTypeid());
				params.put("advisory_fees", userInfoModel.getAdvisory_fees());
				params.put("expert_typeid", userInfoModel.getExpert_typeid());//
				params.put("speciality", userInfoModel.getSpeciality());
				params.put("motto", userInfoModel.getMotto());
				params.put("area_id", userInfoModel.getArea_id());
				params.put("email", userInfoModel.getEmail());
				params.put("sex", userInfoModel.getSex());
				params.put("alipay", userInfoModel.getAlipay());
				params.put("birthday", userInfoModel.getBirthday());
				//    
				params.put("phone", userInfoModel.getPhone());
				params.put("alipayName", userInfoModel.getAlipayName());
				if (XiuGai == 1) {
					try {
						params.put("info_head", new File(path));// "/storage/sdcard1/DCIM/Camera/IMG_20140723_231753.jpg")

					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						//    
						   
					}
					params.put("head", userInfoModel.getHead());
					params.put("uploadBack", "1");
				} else if (XiuGai == 2) {
					try {
						params.put("head", new File(path));// "/storage/sdcard1/DCIM/Camera/IMG_20140723_231742.jpg"

					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						//    
						   
					}
					params.put("info_head", userInfoModel.getInfo_head());// "
					params.put("uploadHead", "1");

				} else {
					params.put("head", userInfoModel.getHead());
					params.put("info_head", userInfoModel.getInfo_head());
				}

				params.put("like_club", userInfoModel.getLike_club());
				params.put("unlike_club", userInfoModel.getUnlike_club());
				params.put("education", userInfoModel.getEducation());

				FinalHttp fh = new FinalHttp();
				fh.post(Constants.BadiUrl + "userDataSet", params,
						new AjaxCallBack<Object>() {

							@Override
							public void onLoading(long count, long current) {
								// TODO Auto-generated method stub
								super.onLoading(count, current);
								//    
								// "current-->"
								// + current);

							}

							@Override
							public void onSuccess(final Object t) {
								// TODO Auto-generated method stub

								Utils.ExitPrgress(context);
								//    

								String content = (String) t;
								//    
								if (content != null) {
									//    
									Results results = Utils.checkResult_NNN(
											context, content);

									if (results != null
											&& results.getRetmsg() != null) {
										//    
										// + results.getRetmsg());
										try {
											Message msg = new Message();
											msg.what = 3;
											Bundle bun = new Bundle();
											bun.putString("Retmsg",
													results.getRetmsg());
											bun.putBoolean("Refresh", refresh);
											msg.setData(bun);

											handler.sendMessage(msg);

										} catch (Exception e) {
											// TODO: handle exception
										}
									} else {
										// Toast.makeText(context, "输出结果为空",
										// 1).show();

										handler.sendEmptyMessage(2);

									}
								} else {
									// Toast.makeText(context,"网络连接出错",
									// 1).show();
									handler.sendEmptyMessage(2);

								}

								super.onSuccess(t);
							}
						});
			}
		}).start();

	}

	/**
	 * 只用来修改质询费（这是登录后右滑的）
	 * 
	 * @param userInfoModel
	 *            UserInfoModel类
	 * @param uid
	 *            用户上传id
	 * @param XiuGai
	 *            1只修改背景图片,2只修改头像,其他数都不修改
	 * @param path
	 *            XiuGai来判定修改哪张图片 path为那张图片的路径，每次修改只能一张
	 * @param refresh
	 *            是否刷新数据(无效)
	 * @param handler
	 *            一个handler 分别是网络错误，系统错误，成功，失败 final int fun_1,final int
	 *            fun_3,final int suc,final int defal
	 */
	public void UserDataSet4(final UserInfoModel userInfoModel,
			final int XiuGai, final String path, final boolean refresh,
			final Handler handler, final int fun_1, final int fun_3,
			final int suc, final int defal) {
		// Utils.showPrgress(context);
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub

				AjaxParams params = new AjaxParams();
				params.put("uid", userInfoModel.getUid());
				params.put("nickName", userInfoModel.getNickName());
				params.put("typeid", userInfoModel.getTypeid());
				params.put("advisory_fees", userInfoModel.getAdvisory_fees());
				params.put("expert_typeid", userInfoModel.getExpert_typeid());//
				params.put("speciality", userInfoModel.getSpeciality());
				params.put("motto", userInfoModel.getMotto());
				params.put("area_id", userInfoModel.getArea_id());
				params.put("email", userInfoModel.getEmail());
				params.put("sex", userInfoModel.getSex());
				params.put("alipay", userInfoModel.getAlipay());
				params.put("birthday", userInfoModel.getBirthday());
				//    
				params.put("phone", userInfoModel.getPhone());
				params.put("alipayName", userInfoModel.getAlipayName());
				if (XiuGai == 1) {
					try {
						params.put("info_head", new File(path));// "/storage/sdcard1/DCIM/Camera/IMG_20140723_231753.jpg")

					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						//    
						   
					}
					params.put("head", userInfoModel.getHead());
					params.put("uploadBack", "1");
				} else if (XiuGai == 2) {
					try {
						params.put("head", new File(path));// "/storage/sdcard1/DCIM/Camera/IMG_20140723_231742.jpg"

					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						//    
						   
					}
					params.put("info_head", userInfoModel.getInfo_head());// "
					params.put("uploadHead", "1");

				} else {
					params.put("head", userInfoModel.getHead());
					params.put("info_head", userInfoModel.getInfo_head());
				}

				params.put("like_club", userInfoModel.getLike_club());
				params.put("unlike_club", userInfoModel.getUnlike_club());
				params.put("education", userInfoModel.getEducation());

				FinalHttp fh = new FinalHttp();
				fh.post(Constants.BadiUrl + "userDataSet", params,
						new AjaxCallBack<Object>() {

							@Override
							public void onLoading(long count, long current) {
								// TODO Auto-generated method stub
								super.onLoading(count, current);
								//    
								// "current-->"
								// + current);

							}

							@Override
							public void onSuccess(final Object t) {
								// TODO Auto-generated method stub

								Utils.ExitPrgress(context);
								//    

								String content = (String) t;
								//    
								if (content != null) {
									//    
									Results results = Utils.checkResult_NNN(
											context, content);

									if (results != null
											&& results.getRetmsg() != null) {
										//    
										// + results.getRetmsg());
										try {

											if (results.isRet()) {
												Message msg = new Message();
												msg.what = suc;
												Bundle bun = new Bundle();
												bun.putString("Retmsg",
														results.getRetmsg());
												bun.putBoolean("Refresh",
														refresh);
												msg.setData(bun);
												handler.sendMessage(msg);
											} else {
												Message msg = new Message();
												msg.what = defal;
												Bundle bun = new Bundle();
												bun.putString("Retmsg",
														results.getRetmsg());
												bun.putBoolean("Refresh",
														refresh);
												msg.setData(bun);
												handler.sendMessage(msg);
											}

										} catch (Exception e) {
											// TODO: handle exception
										}
									} else {
										// Toast.makeText(context, "输出结果为空",
										// 1).show();

										handler.sendEmptyMessage(fun_3);

									}
								} else {
									// Toast.makeText(context,"网络连接出错",
									// 1).show();
									handler.sendEmptyMessage(fun_1);
									// fun_3,int suc,int defal
								}

								super.onSuccess(t);
							}
						});
			}
		}).start();
	}
}
